package zhoug.zui.valuepair;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import zhoug.logging.Logger;
import zhoug.zui.R;

import androidx.annotation.Nullable;

/**
 * 选择框
 * zui_style_ZUIKeyValueSelector
 *
 * @Author HK-LJJ
 * @Date 2020/3/23
 * @Description
 */
public class ZUIKeyValueSelector extends ZUIAbsKeyValueView<View> {
    private static final String TAG = ">>>SelectiveValuePair";
    private static final boolean DEBUG=false;
    private TextView tvValue;
    private ImageView ivIcon;
    private Drawable icon;
    private int iconSize;
    private int iconMarginLeft;

    public ZUIKeyValueSelector(Context context) {
        this(context, null);
    }

    public ZUIKeyValueSelector(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.ZUIKeyValueSelector_theme);
    }

    public ZUIKeyValueSelector(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void parseAttrs(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super.parseAttrs(context, attrs, defStyleAttr, defStyleRes);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZUIKeyValueSelector, defStyleAttr, R.style.ZUIKeyValueSelector_def);
        icon = typedArray.getDrawable(R.styleable.ZUIKeyValueSelector_zui_rightIcon);
        iconSize = typedArray.getDimensionPixelSize(R.styleable.ZUIKeyValueSelector_zui_iconSize, 0);
        iconMarginLeft = typedArray.getDimensionPixelSize(R.styleable.ZUIKeyValueSelector_zui_iconMarginLeft, 0);
        typedArray.recycle();
    }

    @Override
    public View getValueView() {
        if (valueView == null) {
            valueView = LayoutInflater.from(getContext()).inflate(R.layout.zui_selectvaluepair, null);
            tvValue = valueView.findViewById(R.id.tv_value);
            ivIcon = valueView.findViewById(R.id.iv_icon);
            initValue(tvValue);
            if(icon!=null){
                ivIcon.setImageDrawable(icon);
            }
            if(iconSize>0 || iconMarginLeft!=0){
                ViewGroup.LayoutParams layoutParams = ivIcon.getLayoutParams();
                if(iconSize>0){
                    layoutParams.width=iconSize;
                    layoutParams.height=iconSize;
                }
                if(iconMarginLeft!=0){
                    if(layoutParams instanceof MarginLayoutParams){
                        ((MarginLayoutParams) layoutParams).leftMargin=iconMarginLeft;
                    }
                }
                ivIcon.setLayoutParams(layoutParams);
            }

            if (DEBUG) {
                Logger.d(TAG, "ivIcon:"+ivIcon);
                Logger.d(TAG, "iconSize:"+iconSize);
                Logger.d(TAG, "iconMarginLeft:"+iconMarginLeft);
            }
        }

        return valueView;
    }



    @Override
    public String getValue() {
        return tvValue.getText().toString();
    }

    @Override
    public void setValue(CharSequence value) {
        tvValue.setText(value);
        setError(null);
    }

    @Override
    protected void _setStatusColor(int statusColor) {
        if(tvValue!=null){
            tvValue.setTextColor(statusColor);
        }
    }

    @Override
    public void setError(CharSequence error) {
        super.setError(error);
//        tvValue.setError(error);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if(enabled){
            ivIcon.setVisibility(VISIBLE);
        }else{
            ivIcon.setVisibility(GONE);
        }
    }
}
